//
//  JLSMainTabBarController.m
//  看点
//
//  Created by 焦林生 on 15/12/15.
//  Copyright © 2015年 jiaolinsheng. All rights reserved.
//

#import "JLSMainTabBarController.h"
#import "JLSNavigationController.h"
#import "JLSButtomTabBarBtn.h"
#import "JLSButtomTabBar.h"
//代理5 遵守协议
@interface JLSMainTabBarController ()<JLSButtomTabBarDelegate>
/**
 *  tabBar的名称
 */
@property (nonatomic, strong) NSArray *tabBarName;
@end

@implementation JLSMainTabBarController

//懒加载
- (NSArray *)tabBarName
{

    if (_tabBarName == nil) {
        _tabBarName = @[@"新闻", @"阅读", @"视听", @"发现", @"我"];
    }
    return _tabBarName;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //1 初始化子控制器
    [self setupChildViewControllers];
    
    //2 初始化自定义 tabBar
    [self setupCustomTabBar];
    
}

#pragma mark - 初始化子控制器
- (void)setupChildViewControllers
{

    //加载5个 storyboard 中的导航控制器,把导航控制器添加到 tabBar
    //1 新闻中心
    JLSNavigationController *mainHall =[self  navigationControllerWithStoryboardName:@"Main" ];
    
    //2 阅读中心
    JLSNavigationController *reading = [self navigationControllerWithStoryboardName:@"Reading"];
    
    //3 视听中心
    JLSNavigationController *video = [self navigationControllerWithStoryboardName:@"Video"];
    
    //4 发现
    JLSNavigationController *discover = [self navigationControllerWithStoryboardName:@"Discover"];
    
    //5 我
    JLSNavigationController *me = [self navigationControllerWithStoryboardName:@"Me"];
    
    //6 把5个控制器加载到 tabbar 中
    self.viewControllers = @[mainHall,reading,video,discover,me];
    
}

//封装一个根据 storyboard 的文件名快速创建箭头指向的初始化控制器
- (JLSNavigationController *)navigationControllerWithStoryboardName:(NSString *)name {

    //1 加载 storyboard 文件
    UIStoryboard *sb = [UIStoryboard storyboardWithName:name bundle:nil];
    
    //2 创建 storyboard 中箭头指向的初始化控制器,也就是自定义导航控制器
    return [sb instantiateInitialViewController];
    
}

#pragma mark - 初始化自定义底部 TabBar
- (void)setupCustomTabBar
{

    //1 创建自定义的 tabBar
    JLSButtomTabBar *tabBar = [[JLSButtomTabBar alloc] init];
    
    
    //2 通过循环来创建tabBarBtn
    NSUInteger count = self.viewControllers.count;
    for (int i =0; i < count; i++) {
        
        //获取普通图片和选中图片的名称
        NSString *normal = [NSString stringWithFormat:@"TabBar%d",(i + 1)];
        NSString *selected = [NSString stringWithFormat:@"TabBar%dSel",(i + 1)];
        
        //调用内部封装的方法快速创建自定义 tabBar
        [tabBar addButtomTabBarBtnWithImage:normal selected:selected title:self.tabBarName[i]];
    }
    
    //3 设置 frame 并添加到系统的 tabBar
    tabBar.frame = self.tabBar.bounds;
    [self.tabBar addSubview:tabBar];
    
    //代理4 设置代理
    tabBar.delegate = self;
}

//代理6 实现协议方法
- (void)buttomTabBar:(JLSButtomTabBar *)tabBar didClickButtomTabBarWithIndex:(int)index
{
    
    //利用索引 切换控制器
    self.selectedIndex = index;
}




@end
